package bot;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Hashtable;

public class AwfulWaffle {

	private Hashtable<String, WordNode> dictionary;
	private WordNode start;
	private boolean logOn = true;
	
	private int save = 4;
	private int count = 0;
	private ObjectOutputStream output;
	private ObjectInputStream input;
	private FileOutputStream fileOut;
	private FileInputStream fileIn;
	private String directory;
	private File file;

	@SuppressWarnings("unchecked")
	public AwfulWaffle(){
	
		directory = "brain.in";
		file = new File(directory);
		
		try {
			fileOut = new FileOutputStream(file);
			fileIn = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			// File failure
			e.printStackTrace();
		}
		
		try {
			input = new ObjectInputStream(fileIn);
			start = (WordNode) input.readObject();
			dictionary = (Hashtable<String, WordNode>) input.readObject();
			input.close();
		} catch (IOException e) {
			dictionary = new Hashtable<String, WordNode>();
			start = new WordNode();
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			dictionary = new Hashtable<String, WordNode>();
			start = new WordNode();
			e.printStackTrace();
		}	
	}


	// Breaks down a sentence, remove punctuation, and grammar.
	public void readPhrase(String phrase){
		// Extract each word into an array//
		phrase = phrase.toLowerCase();
		String[] words = phrase.split(" ");

		// Add the words
		if(words.length > 0)
			addPhrase(words, 0 , start);
		
		if(++count >= save){
			saveAll(fileOut);
			count = 0;
		}
	}

	private void saveAll(FileOutputStream file) {
		try {
			// Open
			output = new ObjectOutputStream(file);
			
			// Write
			output.writeObject(start);
			output.writeObject(dictionary);
			
			// Close
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}


	// Prepares to add the words (recursively)
	private void addPhrase(String[] words, int index, WordNode parent){
		//log(parent.toString());
		// Checks to see if we have added all words already
		if(index >= words.length) {
			parent.addWord(new WordNode());
			log("\n");
			return;
		}
		
		WordNode child;

		// Check to see if we're linked to the word that we want already
		if(parent.contains(words[index])) {
			child = dictionary.get(words[index].toLowerCase());

			log(child.getWord() + "*--> ");
			
			// Just move to that word
			addPhrase(words, ++index, child);

		// Check to see if the word we're adding exists at all (the dictionary) already
		} else if ( dictionary.containsKey(words[index].toLowerCase())) {
			child = dictionary.get(words[index].toLowerCase());

			// Add the child to the parent
			parent.addWord(child);
			
			log(child.getWord() + "^--> ");

			// Now move to the next word
			addPhrase(words, ++index, child);

		// New word!
		} else {
			// Add the word to the parent
			child = new WordNode(words[index]);
			parent.addWord(child);

			// Add the word to the dictionary
			dictionary.put(child.getWord().toLowerCase(), child);

			log(child.getWord() + "+--> ");
			
			// Now move to the next word
			addPhrase(words, ++index, child);
		}

	}

	public String getSentence(){
		return getPhrase(start.getRanNextWord());
	}
	
	private String getPhrase(WordNode parent){
		if(parent == null || parent.getWord() == null)
			return "";
		
		log(parent.toString() + "\n");
		return parent.getWord() + " " + getPhrase(parent.getRanNextWord());
	}
	
	private void log(String message){
		if(logOn)
			System.out.print(message);
	}
	
	
	
}